package vn.orderonline.web.admin.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import vn.orderonline.com.core.business.restaurant.IRestaurantManager;
import vn.orderonline.com.core.mvc.model.RestaurantDto;

@Controller
@RequestMapping("/restaurant")
public class RestaurantController {
	
	private static final Logger LOGGER = Logger.getLogger(RestaurantController.class);

	@Autowired
	private IRestaurantManager iRestaurantManager;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String getListRestautaurant(ModelMap model) {
		LOGGER.info("GET LIST OF RESTAURANTS!");
		List<RestaurantDto> list = null;
		RestaurantDto objSearch = null;
		try {
			list = iRestaurantManager.getAllRestaurants();
			model.addAttribute("list", list);
			objSearch = new RestaurantDto();
			model.addAttribute("objSearch", objSearch);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN GET LIST OF RESTAURANTS!", e);
		}
		LOGGER.info("END GET LIST OF RESTAURANTS!");
		return "/restaurant/searchrestaurant";
	}
	
	@RequestMapping(value = "/search", method = RequestMethod.POST)
	public ModelAndView search(@ModelAttribute RestaurantDto objSearch) {
		LOGGER.info("START SEARCH RESTAURANT!");
		ModelAndView mv = null;
		try {
			mv = new ModelAndView("/restaurant/searchrestaurant");
			mv.addObject("list", iRestaurantManager.search(objSearch));
			mv.addObject("objSearch", objSearch);
		} catch (Exception e) {
			LOGGER.info("ERROR WHEN SEARCH RESTAURANT!", e);
		}
		LOGGER.info("END SEARCH RESTAURANT!");
		return mv;
	}
	
//	@RequestMapping(value = "/detail/{restaurantId}", method = RequestMethod.GET)
//	public ModelAndView detail(@PathVariable Integer restaurantId) {
//		LOGGER.info("START VIEW DETAIL RESTAURANT!" + "restaurantId = " + restaurantId);
//		ModelAndView mv = null;
//		try {
//			mv = new ModelAndView("/restaurant/detailrestaurant");
//			mv.addObject("res", iRestaurantManager.getRestaurantById(restaurantId));
//		} catch (Exception e) {
//			LOGGER.error("ERROR WHEN VIEW DETAIL RESTAURANT!", e);
//		}
//		LOGGER.info("END VIEW DETAIL RESTAURANT!");
//		return mv;
//	}
//	
	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public String detail(@RequestParam(value="restaurantId", required = true) Integer restaurantId, ModelMap model) {
		LOGGER.info("START VIEW DETAIL RESTAURANT!" + "restaurantId = " + restaurantId);
		try {
			model.addAttribute("res", iRestaurantManager.getRestaurantById(restaurantId));
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN VIEW DETAIL RESTAURANT!", e);
		}
		LOGGER.info("END VIEW DETAIL RESTAURANT!");
		return "/restaurant/detailrestaurant";
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(@RequestParam(value = "restaurantId", required = true) Integer restaurantId) {
		LOGGER.info("START DELETE RESTAURANT!");
		try {
			iRestaurantManager.delete(restaurantId);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN DELETE RESTAURANT!", e);
		}
		LOGGER.info("END DELETE RESTAURANT!");
		return "redirect:/restaurant/list";
	}
	
	@RequestMapping(value="/form")
	public String formAdd(ModelMap model) {
		LOGGER.info("CREATE FORM REGISTER RESTAURANT!");
		try {
			model.addAttribute("res", new RestaurantDto());
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN CREATE FORM REGISTER RESTAURANT!");
		}
		LOGGER.info("END CREATE FORM REGISTER RESTAURANT!");
		return "/restaurant/addrestaurant";
	}
	
	@RequestMapping(value="/add")
	public String create(RestaurantDto res) {
		LOGGER.info("START ADD RESTAURANT!");
		try {
		 	iRestaurantManager.add(res);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN ADD RESTAURANT!", e);
		}
		LOGGER.info("END ADD RESTAURANT!");
		return "redirect:list";
	}
	
	@RequestMapping(value = "/updateform", method = RequestMethod.GET)
	public ModelAndView formUpdate(@RequestParam(value = "restaurantId", required = true) Integer restaurantId) {
		LOGGER.info("START CREATE FORM UPDATE RESTAURANT!");
		ModelAndView mv = null;
		try {
			mv = new ModelAndView("/restaurant/updaterestaurant");
			mv.addObject("res", iRestaurantManager.getRestaurantById(restaurantId));
			iRestaurantManager.getRestaurantById(restaurantId);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN CREATE FORM UPDATE RESTAURANT!", e);
		}
		LOGGER.info("END WHEN CREATE UPDATE FORM UPDATE RESTAURANT!");
		return mv;
	}
	
	@RequestMapping(value = "/update/{restaurantId}", method = RequestMethod.POST)
	public String update(@ModelAttribute RestaurantDto res, @PathVariable Integer restaurantId) {
		LOGGER.info("START UPDATE RESTAURANT!");
		try {
			res.setRestaurantId(restaurantId);
			iRestaurantManager.update(res);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN UPDATE RESTAURANT!", e);
		}
		LOGGER.info("END UPDATE RESTAURANT!");
		return "redirect:/restaurant/list";
	}
	
	@RequestMapping(value="/checkexistedrestaurant")
	public @ResponseBody Boolean checkExistedRestaurant(@RequestParam String restaurantName) {
		LOGGER.info("START CHECK EXISTED RESTAURANT NAME!");
		Boolean flag = false;
		try {
			flag = iRestaurantManager.checkExistedRestaurant(restaurantName);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN CHECK EXISTED RESTAURANT NAME!");
		}
		LOGGER.info("END CHECK EXISTED RESTAURANT NAME!");
		return flag;
	}
	
}
